﻿
using Newtonsoft.Json;
using RTW.OA.Areas.Personal.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.Personal.Controllers
{
    public class NotifyController : Controller
    {
        private NotifyServers notifyServers;

        public NotifyController()
        {
            notifyServers = new NotifyServers();
        }


        // GET: Personal/Notify
        public ActionResult Index()
        {
            ViewBag.TYPE_IDData = notifyServers.GetTYPE_ID();
            ViewBag.TYPE_IDDataJson = JsonConvert.SerializeObject(notifyServers.GetTYPE_ID());
            return View();
        }

        // GET: Personal/Notify/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.TYPE_IDData = notifyServers.GetTYPE_ID();
            var model = new notify();
            if (id != null)
            {
                ViewBag.Title = "编辑公告表";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建公告表";
            }
            return View(model);
        }

        // POST: Personal/Notify/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, notify model)
        {
            ViewBag.TYPE_IDData = notifyServers.GetTYPE_ID();
            try
            {
                if (id == null || id == 0)
                {
                    notifyServers.Add(model);
                    ViewBag.msg = "新增公告表信息成功！";
                    model = new notify();

                }
                else
                {
                    notifyServers.Update(model);
                    ViewBag.msg = "修改公告表信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Personal/Notify/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                notifyServers.Del(id.ToString());
                ViewBag.msg = "删除公告表信息成功！";
            }
            return Content("");
        }


        #region //GetData
        public string GetTableJsonResult(
                                      string SEND_TIME,
                                           string TYPE_ID
                                      )
        {
            var list = notifyServers.GetData(SEND_TIME, TYPE_ID);
            return JsonConvert.SerializeObject(list);
        }

        public notify GetModel(string id)
        {
            var model = notifyServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}